package com.adapter.adapterUsedInMVC;

import java.util.ArrayList;
import java.util.List;

public class DispatcherServlet {

    public static List<HandlerAdapter> handlerAdapterList = new ArrayList<>();

    //初始化的时候把所有的适配器放到集合中去
    public DispatcherServlet(){
        handlerAdapterList.add(new HttpHandlerAdapter());
        handlerAdapterList.add(new SimpleHandlerAdapter());
        handlerAdapterList.add(new AnnotationHandlerAdapter());
    }

    public void doDispatch(){
        //这里模拟springmvc通过request对象，得到handler对象 ，其实就是controller对象
        //通过是配置可得到希望得到的Controller对象，而不用使用很多的ifelse判断
        HttpController controller = new HttpController();
        HandlerAdapter handlerAdapter = getHandler(controller);
        handlerAdapter.handle(controller);
    }

    public HandlerAdapter getHandler(Controller controller){
        for (HandlerAdapter handlerAdapter : handlerAdapterList) {
                if (handlerAdapter.supports(controller)) return handlerAdapter ;
        }
        return  null ;
    };

}
